﻿using System.Security.Cryptography;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Wips.AppService.Publisher.Requests;

namespace Wips.MainServer.Controllers
{

    /// <summary>
    /// 服务器发布相关服务
    /// </summary>
    public class MasterController : CustomeBaseController
    {

        private readonly IMediator _mediator;


        /// <summary>
        /// 构造
        /// </summary>
        public MasterController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 注册平台
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<string> RegisterAgentAsync(RegisterAgentServerRequest request)
        {
            return await _mediator.Send(request);
        }

        public static void GenerateKeys(out string publicKey, out string privateKey)
        {
            var rsa = new RSACryptoServiceProvider(2048);  // 2048是密钥的位数，可以根据需要调整
            publicKey = rsa.ToXmlString(false);  // 获取公钥
            privateKey = rsa.ToXmlString(true);  // 获取私钥
        }
        [HttpGet]
        public async Task<string> GetRsa2String()
        {
            // 使用方法
            string publicKey, privateKey;
            GenerateKeys(out publicKey, out privateKey);

            return "Public Key: " + publicKey + ";" + "Private Key: " + privateKey;


        }

    }
}
